#! /bin/bash
# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4:

# Regenerate the reference files for toktx-tests.

# Copyright 2017 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

# Depth of this script relative to the project root
depth=../..

# Change dir to the testimages folder, the script location...
cd $(dirname $0)

# Make paths relative to the testimages directory.
ktx_root=$depth
toktx_vs2013=$ktx_root/build/msvs/win/vs2013/x64/Release/toktx.exe
toktx_vs2015=$ktx_root/build/msvs/win/vs2015/x64/Release/toktx.exe
toktx_cmake=$ktx_root/build/cmake/linux/Release/toktx
toktx_cmake_d=$ktx_root/build/cmake/linux/Debug/toktx
toktx_make=$ktx_root/build/make/linux/out/Release/toktx
toktx_make_d=$ktx_root/build/make/linux/out/Debug/toktx

# Ensure generation is not polluted by user environment
unset TOKTX_OPTIONS

if [ -n "$1" -a -x "$1" ]; then
  toktx="$1"
elif [ -x "$toktx_vs2013" ]; then
  toktx=$toktx_vs2013
elif [ -x "$toktx_vs2015" ]; then
  toktx=$toktx_vs2015
elif [ -x "$toktx_cmake" ]; then
  toktx=$toktx_cmake
elif [ -x "$toktx_cmake_d" ]; then
  toktx=$toktx_cmake_d
elif [ -x "$toktx_make" ]; then
  toktx=$toktx_gmake
elif [ -x "$toktx_make_d" ]; then
  toktx=$toktx_gmake
elif which toktx >/dev/null; then
  toktx=toktx
else
  echo $0: None of $toktx_vs2013, $toktx_vs2015, $toktx_gmake or $toktx_cmake found.
  echo $0: Nor was toktx found in along PATH.
  echo $0: Aborting generation
  exit 1
fi

# Generate the files ...

$toktx --lower_left_maps_to_s0t0 --nometadata rgb-reference.ktx ../srcimages/rgb.ppm
# The purpose of this is testing automatic mipmap generation. Since SRGB is
# not color renderable on ES3 or WebGL, therefore AMG won't work, use linear.
$toktx --automipmap --lower_left_maps_to_s0t0 --nometadata --linear rgb-amg-reference.ktx ../srcimages/rgb.ppm
$toktx --lower_left_maps_to_s0t0 --nometadata orient-up.ktx ../srcimages/up.ppm
$toktx --lower_left_maps_to_s0t0 orient-up-metadata.ktx ../srcimages/up.ppm
$toktx orient-down-metadata.ktx ../srcimages/up.ppm
$toktx --lower_left_maps_to_s0t0 --mipmap --nometadata rgb-mipmap-reference.ktx ../srcimages/level0.ppm ../srcimages/level1.ppm ../srcimages/level2.ppm ../srcimages/level3.ppm ../srcimages/level4.ppm ../srcimages/level5.ppm ../srcimages/level6.ppm
$toktx --lower_left_maps_to_s0t0 --nometadata rgba-reference.ktx ../srcimages/rgba.pam
$toktx --bcmp --test alpha_simple_basis.ktx2 ../srcimages/alpha_simple.png
$toktx --bcmp --test kodim17_basis.ktx2 ../srcimages/kodim17.png
$toktx --bcmp --test color_grid_basis.ktx2 ../srcimages/color_grid.png
$toktx --t2 --bcmp --mipmap --test rgba-mipmap-reference-basis.ktx2 ../srcimages/level0-alpha.pam ../srcimages/level1-alpha.pam ../srcimages/level2-alpha.pam ../srcimages/level3-alpha.pam ../srcimages/level4-alpha.pam ../srcimages/level5-alpha.pam ../srcimages/level6-alpha.pam
$toktx --uastc --genmipmap --test cimg5293_uastc.ktx2 ../srcimages/CIMG5293.jpg
$toktx --zcmp --uastc --genmipmap --test cimg5293_uastc_zstd.ktx2 ../srcimages/CIMG5293.jpg
$toktx --nowarn --bcmp --test camera_camera_BaseColor.ktx2 ../srcimages/camera_camera_BaseColor_16bit.png
$toktx --nowarn --bcmp --test CesiumLogoFlat.ktx2 ../srcimages/CesiumLogoFlat_palette.png

# ... and additional files for the load tests.

$toktx --lower_left_maps_to_s0t0 rgb-reference-metadata.ktx ../srcimages/rgb.ppm
